#include "rclcpp/rclcpp.hpp"

class ParamServer : public rclcpp::Node
{
public:
    // 允许删除参数
    ParamServer() : Node("demo_param_server", rclcpp::NodeOptions().allow_undeclared_parameters(true))
    {
        RCLCPP_INFO(this->get_logger(), "节点已经启动!");
    }
    // 增
    void add_param()
    {
        this->declare_parameter("name", "上料点");
        this->declare_parameter("x", 0.0);
        this->declare_parameter("y", 0.0);
        this->set_parameter(rclcpp::Parameter("z", 1.0));
        RCLCPP_INFO(this->get_logger(), "add_param!");
    }
    // 删
    void del_param()
    {
        RCLCPP_INFO(this->get_logger(), "del_param!");
        this->undeclare_parameter("z");
        RCLCPP_INFO(this->get_logger(), "删除后是否包含 %d", this->has_parameter("z"));
    }
    // 改
    void update_param()
    {
        RCLCPP_INFO(this->get_logger(), "update_param!");
        this->set_parameter(rclcpp::Parameter("name","下料点"));
    }
    // 查
    void get_param()
    {
        RCLCPP_INFO(this->get_logger(), "------------get_param!");
        auto name = this->get_parameter("name");
        RCLCPP_INFO(this->get_logger(), "name key=%s,value=%s,type=%s",
                    name.get_name().c_str(),
                    name.as_string().c_str(),
                    name.get_type_name().c_str());
        auto params = this->get_parameters({"name", "x", "y", "z"});
        for (auto &param : params)
        {
            RCLCPP_INFO(this->get_logger(), "key=%s,value=%s,type=%s",
                        param.get_name().c_str(),
                        param.value_to_string().c_str(),
                        param.get_type_name().c_str());
        }
    }

private:
};

int main(int argc, char *argv[])
{
    /* 初始化rclcpp  */
    rclcpp::init(argc, argv);
    /* 产生一个 节点 */
    auto node = std::make_shared<ParamServer>();
    node->add_param();
    node->del_param();
    node->update_param();
    node->get_param();
    /* 运行节点，并检测退出信号 Ctrl+C */
    rclcpp::spin(node);
    /* 停止运行 */
    rclcpp::shutdown();
    return 0;
}